package com.freshcity.component
{
	import com.freshcity.manager.CommunicatorManager;
	import com.freshcity.core.OpCodes;
	import com.pblabs.core.PBComponent;
	import com.pblabs.core.PBGameObject;
	import com.pblabs.debug.Logger;
	import com.pblabs.time.ITicked;
	import com.pblabs.time.TimeManager;
	
	import fl.controls.Label;
	
	import flash.display.Sprite;
	import flash.utils.getTimer;

	public class BusTimeGameObject extends BaseGameObject implements ITicked
	{
		private var pb:Label;
		private var lastRunTime:int;
		public var runTime:int;
		private var lastTime:int;
		
		override public function destroy():void
		{
			// TODO Auto Generated method stub
			super.destroy();
		}
		
		override public function initialize():void
		{
			super.initialize();
			(this.owningGroup.getManager(TimeManager) as TimeManager).addTickedObject(this);
			
			pb = new Label();
			pb.setSize(100,30);
			parent.addChild(pb);
			
			group.commu = this.owningGroup.getManager(CommunicatorManager) as CommunicatorManager;
			group.commu.register(OpCodes.SYN_TIME,onUpdateTime);
		}
		
		private function onUpdateTime(data:Object):void
		{
			var synTime:int = int(data);
			Logger.debug(this,"Before runTime vs synTime",runTime + ":" +synTime);
			Logger.debug(this,"Interval",String(runTime-lastRunTime));
			lastRunTime = runTime;
			runTime= synTime;
			//pb.text = String(runTime);
			Logger.debug(this,"After runTime vs synTime",runTime + ":" +synTime);
			pb.text = int(runTime/60) + ":" + int(runTime%60);
		}		
		
		public function onTick():void
		{
			if (this.lastTime <= 0)
			{
				this.lastTime = getTimer();
				return;
			}else if (getTimer() - this.lastTime >= 1000)
			{
				runTime ++;
				//pb.text = int(runTime/data.inteval*100) + "%";
				pb.text = int(runTime/60) + ":" + int(runTime%60);
				//pb.text = String(runTime);
				this.lastTime = getTimer();
			}
		}
	}
}